package www.bplead.com.mydemo.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import www.bplead.com.controller.action.student.entity.Student;
import www.bplead.com.controller.action.student.event.StudentQueryRequest;
import www.bplead.com.controller.action.student.event.StudentQueryResponse;
import www.bplead.com.mydemo.R;
import www.bplead.com.mydemo.adapter.StudentInfoAdapter;

/**
 * Created by wangyangzi on 2016/10/19.
 */
public class StudentListActivity extends BaseActivity implements View.OnClickListener {
    private static final String TAG = "StudentListActivity";

    TextView funTV;
    ListView stuInfoListView;
    StudentInfoAdapter studentInfoAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.student_list);

        funTV = (TextView) this.findViewById(R.id.fun_add_tv);
        stuInfoListView = (ListView) this.findViewById(R.id.stu_info_lv);

        funTV.setOnClickListener(this);
        sendRequest();
    }

    private void sendRequest(){
        StudentQueryRequest request = new StudentQueryRequest();
        EventBus.getDefault().post(request);
    }

    int ADD_REQUEST_CODE = 1001;
    int SAVE_RESULT_CODE = 1000;
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode == ADD_REQUEST_CODE && resultCode == SAVE_RESULT_CODE){
            Student stu = (Student) data.getSerializableExtra("student");
            studentInfoAdapter.getStudentList().add(0,stu);
            studentInfoAdapter.notifyDataSetChanged();
        }
    }

    @Subscribe
    public void getStudentList(StudentQueryResponse response){
        if(response.result){
            studentInfoAdapter = new StudentInfoAdapter(this,response.studentList);
            stuInfoListView.setAdapter(studentInfoAdapter);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.fun_add_tv:
                Intent intent1 = new Intent(this,StudentInfoActivity.class);
                startActivityForResult(intent1,ADD_REQUEST_CODE);
                this.overridePendingTransition(R.anim.activity_translate_in,R.anim.activity_hide);
                break;
        }
    }
}
